def sign(x):
    """返回x的符号，1表示正数，-1表示负数，0表示零"""
    return (x > 0) - (x < 0)


def trend_similarity(target_sequence, candidate_sequence):
    """计算两个序列在每个维度上的变化趋势相似度"""
    # 计算每个维度上的差值
    differences = [t - c for t, c in zip(target_sequence, candidate_sequence)]
    # 计算每个维度上的变化趋势（符号）
    target_trends = [sign(d) for d in differences]
    # 为了比较趋势，我们需要一个参考点，这里选择第一个元素作为参考
    reference_trend = [sign(t - target_sequence[0]) for t in target_sequence]

    # 计算趋势匹配的数量
    match_count = sum(1 for t, r in zip(target_trends, reference_trend) if t == r)

    # 返回匹配的数量，匹配数量越多，趋势越接近
    return match_count


def find_closest_trend(target_sequence, data_sequences):
    # 初始化最大相似度为-1，最接近的序列为None
    max_similarity = -1
    closest_sequence = None

    # 遍历所有数据序列
    for sequence in data_sequences:
        # 计算当前序列与目标序列的变化趋势相似度
        similarity = trend_similarity(target_sequence, sequence)

        # 如果当前相似度大于最大相似度，则更新最大相似度和最接近的序列
        if similarity > max_similarity:
            max_similarity = similarity
            closest_sequence = sequence

    # 返回与目标序列变化趋势最接近的序列
    return closest_sequence


# 示例数据
target_sequence = [4.25, 4.25, 1.52, 2.15, 3.85, 2.47]
data_sequences = [
    [4.26, 4.24, 1.53, 2.14, 3.84, 2.46],
    [4.24, 4.26, 1.51, 2.16, 3.86, 2.48],
    [4.15, 4.35, 1.42, 2.25, 3.95, 2.57],
    [4.35, 4.15, 1.62, 2.05, 3.75, 2.37]
]

# 调用函数
closest_sequence = find_closest_trend(target_sequence, data_sequences)
print("最接近的序列:", closest_sequence)